package com.zzsy.carosshopprojects.activitys.home.mainpage.common;

import android.content.Context;
import android.util.Log;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.PayKindAdapter;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.CreateOrderBean;
import com.zzsy.carosshopprojects.bean.OrderResultBean;
import com.zzsy.carosshopprojects.bean.PayKindBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.DataUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.lifecycle.MutableLiveData;

public class CollectionMode extends BaseViewMode {
    private int[] icon = {R.mipmap.button_wechat_normal,R.mipmap.button_alipay_normal, R.mipmap.button_unionpay_reghter};
    private String[] alias = {"微信支付", "支付宝支付", "银联支付"};
    private String[] type = {PayKindAdapter.WX,PayKindAdapter.ZFB,PayKindAdapter.YL};
    private MutableLiveData<List<PayKindBean>> liveData;
    private MutableLiveData<OrderResultBean> liveOrder;

    public MutableLiveData<List<PayKindBean>> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    public MutableLiveData<OrderResultBean> getLiveOrderData() {
        if (liveOrder == null)
            liveOrder = new MutableLiveData<>();
        return liveOrder;
    }

    /**
     * 设置支付方式的数据
     */
    public void setPayData() {
        List<PayKindBean> list = new ArrayList<>();
        for (int i = 0; i < icon.length; i++) {
            PayKindBean payKindBean = new PayKindBean();
            payKindBean.setPic(icon[i]);
            payKindBean.setTitle(alias[i]);
            payKindBean.setType(type[i]);
            list.add(payKindBean);
        }
        liveData.postValue(list);
    }

    /**
     * 去支付
     * type 1，支付宝 2. 微信 3 银行卡
     *
     * @param context
     */
    public void toPay(Context context, String type, String amount, String productId, String productInfoId, String usedOil, String oilsPrice) {

        RequestUtils.getToken(context, new MyObserver<BaseBean>(context, true) {
            @Override
            public void onSuccess(BaseBean result) {
                Log.d("xxxxxxxxxx", "BBBBBBBBBBB");
            }

            @Override
            public void onFailure(Throwable e, String tokenMsg, int code) {
                Log.d("xxxxxxxxxx", "AAAAAAAAAAAAAAAAAA");
                if (code == ConfigureFile.code) {
                    String orderAmount = ConverterHelper.toBigDecimal(amount).multiply(ConverterHelper.toBigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).toString();
                    BigDecimal bigDecimal = ConverterHelper.toBigDecimal(orderAmount).divide(ConverterHelper.toBigDecimal(oilsPrice), 2, BigDecimal.ROUND_HALF_UP);
                    String riseNew = bigDecimal.toString();
                    Map<String, String> map = new HashMap<>();
                    map.put("idempotenceToken", tokenMsg);
                    map.put("activityReduction", "0");
                    map.put("memberId", "0");
                    map.put("orderAmount", orderAmount);
                    map.put("orderSource", "3");
                    map.put("payAmount", orderAmount);
                    map.put("payType", type);
                    map.put("productId", productId);
                    //   map.put("productInfoId", productInfoId==null?"0":productInfoId);
                    map.put("terminalId", DataUtil.getInstance().getTerminalId());
                    map.put("oilNum", usedOil);
                    map.put("storeId", DataUtil.getInstance().getStoreId());
                    map.put("oilPrice", oilsPrice);
                    map.put("companyId", DataUtil.getInstance().getCompanyId());
                    map.put("rise", riseNew);
                    Log.d("xxxxx", map.toString());
                    CreateOrder(context, map);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), tokenMsg);
                }

            }
        });
    }

    /**
     * 创建支付订单
     *
     * @param context
     * @param map
     */
    private void CreateOrder(Context context, Map<String, String> map) {
        RequestUtils.CreateOrder(context, map, new MyObserver<CreateOrderBean>(context, true) {
            @Override
            public void onSuccess(CreateOrderBean bean) {
                OrderResultBean orderResultBean = new OrderResultBean();
                if (map.get("payType").equals("1"))
                    orderResultBean.setType(ConfigureFile.ZFBPAY);
                else if (map.get("payType").equals("2"))
                    orderResultBean.setType(ConfigureFile.WXPAY);
                else
                    orderResultBean.setType(map.get("payType"));
                orderResultBean.setOrderNumber(bean.getOrderNumber());
                orderResultBean.setPayAmount(bean.getPayAmount());
                liveOrder.postValue(orderResultBean);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), "订单创建异常");
                liveOrder.postValue(null);
            }
        });
    }


}
